package net.huashitong.interceptor;

import net.huashitong.ssydt.provider.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author huiyang.yu
 */
public class LogInterceptor implements HandlerInterceptor {

    private static Logger logger = LoggerFactory.getLogger(LogInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        logger.info("[{}] [{}] [{}] [{}] [{}] [{}]",
                "RequestStart",
                request.getMethod(),
                request.getRequestURI(),
                StringUtils.defaultString(request.getQueryString()),
                request.getRemoteAddr(),
                request.getHeader("User-Agent"));
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest,
                           HttpServletResponse httpServletResponse, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response, Object handler, Exception exception) throws Exception {
        logger.info("[{}] [{}] [{}] [{}] [{}] [{}]",
                "RequestEnd",
                request.getMethod(),
                request.getRequestURI(),
                StringUtils.defaultString(request.getQueryString()),
                request.getRemoteAddr(),
                request.getHeader("User-Agent"));
    }
}
